home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Orlando_1993 / Devcon93.4 / Networking2 / AS225 / examples / timed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-11  |  4.1 KB  |  200 lines

  1. /*------------------------------------------------------------------------
  2.  * timed.c     -    timed server for the Amiga. Workds with inetd  >= 1.2
  3.  *
  4.  * (c) Copyright 1992 Commodore-Amiga, Inc.  All rights reserved.
  5.  *
  6.  * This software is provided as-is and is subject to change; no warranties
  7.  * are made.  All use is at your own risk.  No liability or responsibility
  8.  * is assumed.
  9.  *------------------------------------------------------------------------
  10.  */
  11.  
  12. #include <ss/socket.h>
  13. #include <dos/dos.h>
  14. #include <proto/all.h>
  15. #include <clib/alib_stdio_protos.h>
  16. #include <string.h>
  17. #include <sys/syslog.h>
  18.  
  19. #define TEMPLATE    "NAME/A,SOCKPTR/A/N,ID/A/N,ARGS/F" 
  20. #define OPT_NAME    0
  21. #define OPT_SOCKPTR    1
  22. #define OPT_ID        2
  23. #define OPT_ARGS    3
  24. #define OPT_COUNT    4
  25.  
  26. void machtime_dg(int);
  27. void daytime_dg (int);
  28. void machtime_stream(int);
  29. void daytime_stream(int);
  30.  
  31. struct Library *SockBase ;
  32. BPTR debugfh;
  33.  
  34. struct inetmsg {
  35.     struct Message    msg;
  36.     ULONG    id;
  37. };
  38.  
  39. void main(void)
  40. {
  41.     int errno, s;
  42.     long opts[OPT_COUNT];
  43.     struct RDargs *rdargs;
  44.     struct inetmsg inet_message;
  45.     struct MsgPort *msgport, *replyport;
  46.  
  47.     memset((char *)opts, 0, sizeof(opts));
  48.     rdargs = ReadArgs(TEMPLATE, opts, NULL);
  49.     if(rdargs = NULL)
  50.     {
  51.         exit(20) ;
  52.     }
  53.     
  54.  
  55.     /* socket.library version >= 4.0 required for s_inherit() */
  56.     
  57.     if ((SockBase = OpenLibrary( "inet:libs/socket.library", 4L ))==NULL) 
  58.     {
  59.         goto exit1;
  60.     }
  61.     setup_sockets(5,&errno);
  62.  
  63.     /* now get our socket */ 
  64.     s = s_inherit((void *)*(long *)opts[OPT_SOCKPTR]);
  65.     
  66.     if(opts[OPT_ARGS] && *(char *)opts[OPT_ARGS]=='U') 
  67.     {
  68.         if(stricmp((char *)opts[OPT_NAME],"time")) 
  69.             daytime_dg(s);
  70.         else
  71.             machtime_dg(s);
  72.     } 
  73.     else 
  74.     {
  75.         if(stricmp((char *)opts[OPT_NAME],"time"))
  76.             daytime_stream(s);
  77.         else
  78.             machtime_stream(s);
  79.     } 
  80.     s_close(s);
  81.  
  82.     /* if id was nonzero, then we have to inform inetd that we are done */
  83.     if(inet_message.id = *(long *)opts[OPT_ID]) 
  84.     {
  85.         replyport = CreateMsgPort();
  86.         if(replyport==NULL) 
  87.         {
  88.             s_syslog(LOG_ERR,"TIMED: Couldn't create reply port\n");
  89.             goto exit3;
  90.         }
  91.         inet_message.msg.mn_Node.ln_Type = NT_MESSAGE;
  92.         inet_message.msg.mn_Length = sizeof(struct inetmsg);
  93.         inet_message.msg.mn_ReplyPort = replyport;
  94.  
  95.         Forbid();
  96.         msgport = FindPort("inetd");
  97.         if(msgport==NULL)  
  98.         {
  99.             Permit();
  100.             s_syslog(LOG_ERR,"TIMED: Couldn't find inetd port\n");
  101.             DeleteMsgPort(replyport);
  102.             goto exit3;
  103.         }
  104.         PutMsg(msgport,(struct Message *)&inet_message);
  105.         Permit();
  106.         /* we can't exit until we received a reply */
  107.         (void)WaitPort(replyport);
  108.         DeleteMsgPort(replyport);
  109.     }    
  110.  
  111. exit3:
  112.     cleanup_sockets();
  113. exit2:
  114.     CloseLibrary(SockBase);
  115. exit1:
  116.     Close(debugfh);
  117.     FreeArgs(rdargs);
  118.     exit(0) ;
  119. }
  120.  
  121.  
  122. void daytime(char *str)
  123. {
  124.     struct DateTime dt;
  125.     char day[20], date[20], time[20];
  126.  
  127.     dt.dat_Format = FORMAT_DOS;
  128.     dt.dat_Flags = 0;
  129.     dt.dat_StrDay = day;
  130.     dt.dat_StrTime = time;
  131.     dt.dat_StrDate = date;
  132.     (void)DateStamp(&dt.dat_Stamp);
  133.     (void)DateToStr(&dt);
  134.     sprintf(str,"%s %s %s\r\n",day,date,time);
  135. }
  136.  
  137.  
  138. /* Return human-readable time of day */
  139. void daytime_stream(int s)
  140. {
  141.     char buffer[256];
  142.     daytime(buffer);
  143.     (void)send(s, buffer, strlen(buffer),0);
  144. }
  145.  
  146. void daytime_dg(int s)
  147. {
  148.     char buffer[256];
  149.     struct sockaddr sa;
  150.     int size;
  151.  
  152.     size = sizeof(sa);
  153.     if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
  154.         return;
  155.     daytime(buffer);
  156.     (void)sendto(s, buffer, strlen(buffer), 0, &sa, sizeof(sa));
  157. }
  158.  
  159.  
  160. /*
  161.  * Return a machine readable date and time, in the form of the
  162.  * number of seconds since midnight, Jan 1, 1900.  Since DateStamp
  163.  * returns the number of seconds since midnight, Jan 1, 1978,
  164.  * we must add 246144960 seconds.
  165.  */
  166.  
  167.  
  168. long machtime(void)
  169. {
  170.     struct DateStamp ds;
  171.     long t;
  172.  
  173.     (void)DateStamp(&ds);
  174.     t = ds.ds_Days*86400 + ds.ds_Minute*60 + (long)(ds.ds_Tick/50);
  175.     t += 2461449600;
  176.     return( t );
  177. }
  178.  
  179.  
  180. void machtime_stream(int s)
  181. {
  182.     long result;
  183.  
  184.     result = machtime();
  185.     (void) send(s, (char *) &result, sizeof(result),0);
  186. }
  187.  
  188. void machtime_dg(int s)
  189. {
  190.     long result;
  191.     struct sockaddr sa;
  192.     int size;
  193.  
  194.     size = sizeof(sa);
  195.     if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
  196.         return;
  197.     result = machtime();
  198.     (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
  199. }
  200.